home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example11.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  7KB  |  232 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example11.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrate how to copy rectangular memory areas */
  21. /* with help of the blitter.                                     */
  22.  
  23.  
  24.  
  25. #include <intuition/intuition.h>
  26. #include <graphics/gfxbase.h>
  27. #include <graphics/gfxmacros.h>
  28.  
  29.  
  30. /* NOTE! We must include the file "gfxmacros.h" inorder to be able to */
  31. /* use the function (macro) SetDrPt().                                */
  32.  
  33.  
  34. #define WIDTH  320 /* 320 pixels wide (low resolution)             */
  35. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display) */ 
  36. #define DEPTH    2 /* 2 BitPlanes should be used, gives 4 colours. */
  37. #define COLOURS  4 /* 2^2 = 4                                      */
  38.  
  39.  
  40. struct IntuitionBase *IntuitionBase;
  41. struct GfxBase *GfxBase;
  42.  
  43.  
  44. struct View my_view;
  45. struct View *my_old_view;
  46. struct ViewPort my_view_port;
  47. struct RasInfo my_ras_info;
  48. struct BitMap my_bit_map;
  49. struct RastPort my_rast_port;
  50.  
  51.  
  52. UWORD my_color_table[] =
  53. {
  54.   0x000, /* Colour  0, Black      */
  55.   0x555, /* Colour  1, Dark grey  */
  56.   0x777, /* Colour  2, Grey       */
  57.   0x999, /* Colour  3, Light grey */
  58. };
  59.  
  60.  
  61. void clean_up();
  62. void main();
  63.  
  64.  
  65. void main()
  66. {
  67.   UWORD *pointer;
  68.   int loop;
  69.   int x, y;
  70.   
  71.  
  72.   /* Open the Intuition library: */
  73.   IntuitionBase = (struct IntuitionBase *)
  74.     OpenLibrary( "intuition.library", 0 );
  75.   if( !IntuitionBase )
  76.     clean_up( "Could NOT open the Intuition library!" );
  77.  
  78.   /* Open the Graphics library: */
  79.   GfxBase = (struct GfxBase *)
  80.     OpenLibrary( "graphics.library", 0 );
  81.   if( !GfxBase )
  82.     clean_up( "Could NOT open the Graphics library!" );
  83.  
  84.  
  85.   /* Save the current View, so we can restore it later: */
  86.   my_old_view = GfxBase->ActiView;
  87.  
  88.  
  89.   /* 1. Prepare the View structure, and give it a pointer to */
  90.   /*    the first ViewPort:                                  */
  91.   InitView( &my_view );
  92.   my_view.ViewPort = &my_view_port;
  93.  
  94.  
  95.   /* 2. Prepare the ViewPort structure, and set some important values: */
  96.   InitVPort( &my_view_port );
  97.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  98.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  99.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  100.   my_view_port.Modes = NULL;           /* Low resolution.               */
  101.  
  102.  
  103.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  104.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  105.   if( my_view_port.ColorMap == NULL )
  106.     clean_up( "Could NOT get a ColorMap!" );
  107.  
  108.   /* Get a pointer to the colour map: */
  109.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  110.  
  111.   /* Set the colours: */
  112.   for( loop = 0; loop < COLOURS; loop++ )
  113.     *pointer++ = my_color_table[ loop ];
  114.  
  115.  
  116.   /* 4. Prepare the BitMap: */
  117.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  118.  
  119.   /* Allocate memory for the Raster: */ 
  120.   for( loop = 0; loop < DEPTH; loop++ )
  121.   {
  122.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  123.     if( my_bit_map.Planes[ loop ] == NULL )
  124.       clean_up( "Could NOT allocate enough memory for the raster!" );
  125.  
  126.     /* Clear the display memory with help of the Blitter: */
  127.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  128.   }
  129.  
  130.   
  131.   /* 5. Prepare the RasInfo structure: */
  132.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  133.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  134.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  135.                                     /* of the display.                   */
  136.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  137.                                     /* RasInfo structure is necessary.   */
  138.  
  139.   /* 6. Create the display: */
  140.   MakeVPort( &my_view, &my_view_port );
  141.   MrgCop( &my_view );
  142.  
  143.  
  144.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  145.   InitRastPort( &my_rast_port );
  146.   my_rast_port.BitMap = &my_bit_map;
  147.   
  148.  
  149.   /* 8. Show the new View: */
  150.   LoadView( &my_view );
  151.  
  152.  
  153.   SetDrMd( &my_rast_port, JAM1 ); /* Use FgPen only.   */
  154.  
  155.   SetAPen( &my_rast_port, 1 );    /* Dark grey */
  156.   Move( &my_rast_port, 10, 10 );
  157.   Draw( &my_rast_port, 26, 10 );
  158.   Draw( &my_rast_port, 26, 26 );
  159.  
  160.   SetAPen( &my_rast_port, 3 );    /* Light grey */
  161.   Draw( &my_rast_port, 10, 26 );
  162.   Draw( &my_rast_port, 10, 10 );
  163.  
  164.   SetAPen( &my_rast_port, 2 );    /* Grey */
  165.   RectFill( &my_rast_port, 11, 11, 25, 25 );
  166.   WritePixel( &my_rast_port, 10, 10 );
  167.   WritePixel( &my_rast_port, 26, 26 );
  168.  
  169.   SetAPen( &my_rast_port, 1 );    /* Dark grey */
  170.   WritePixel( &my_rast_port, 13, 13 );
  171.   WritePixel( &my_rast_port, 23, 13 );
  172.   WritePixel( &my_rast_port, 23, 23 );
  173.   WritePixel( &my_rast_port, 13, 23 );
  174.  
  175.  
  176.   /* We will now make 150 copies of the brick: */
  177.   for( x = 0; x < 15; x++ )
  178.     for( y = 0; y < 10; y++ )
  179.       BltBitMap(
  180.         &my_bit_map, /* Source                 */
  181.         10, 10,      /* Position, source.      */
  182.         &my_bit_map, /* Destination.           */
  183.         50 + 17 * x, /* Position, destination. */
  184.         10 + 17 * y, /*          - " -         */
  185.         17, 17,      /* Width and height.      */
  186.         0xC0,        /* Normal copy.           */
  187.         0xFF,        /* All bitplanes.         */
  188.         NULL );      /* No temporary storage.  */
  189.  
  190.  
  191.   /* Wait 20 seconds: */
  192.   Delay( 50 * 20 );
  193.  
  194.  
  195.   /* 9. Restore the old View: */
  196.   LoadView( my_old_view );
  197.  
  198.  
  199.   /* Free all allocated resources and leave. */
  200.   clean_up( "THE END" );
  201. }
  202.  
  203.  
  204. /* Returns all allocated resources: */
  205. void clean_up( message )
  206. STRPTR message;
  207. {
  208.   int loop;
  209.  
  210.   /* Free automatically allocated display structures: */
  211.   FreeVPortCopLists( &my_view_port );
  212.   FreeCprList( my_view.LOFCprList );
  213.   
  214.   /* Deallocate the display memory, BitPlane for BitPlane: */
  215.   for( loop = 0; loop < DEPTH; loop++ )
  216.     if( my_bit_map.Planes[ loop ] )
  217.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  218.  
  219.   /* Deallocate the ColorMap: */
  220.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  221.  
  222.   /* Close the Graphics library: */
  223.   if( GfxBase ) CloseLibrary( GfxBase );
  224.  
  225.   /* Close the Intuition library: */
  226.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  227.  
  228.   /* Print the message and leave: */
  229.   printf( "%s\n", message ); 
  230.   exit();
  231. }
  232.